package com.xy.datastruc.graph;


import java.util.Arrays;

public class Matrixgraph {
    Vertex[] vertexs;
    int currentSize;
    int[][] edges;

    public Matrixgraph(int n) {
        edges = new int[n][n];
        vertexs = new Vertex[n];
    }

    public void addVertex(Vertex vertex) {
        vertexs[currentSize++] = vertex;
    }

    public void addAdge(int start, int end) {
        System.out.println("start:" + start + " end: " + end);
        //无向图反方向也是通的
        edges[start][end] = 1;
        edges[end][start] = 1;

    }

    public void addAdge(String start, String end) {
        int startindex = 0;
        for (int i = 0; i < vertexs.length; i++) {
            if (start.equals(vertexs[i].value)) {
                startindex = i;
                break;
            }
        }
        int endindex = 0;
        for (int i = 0; i < vertexs.length; i++) {
            if (end.equals(vertexs[i].value)) {
                endindex = i;
                break;
            }
        }
        addAdge(startindex, endindex);
    }


    public void printVertex() {
        if (currentSize == 0) {
            return;
        }
        for (Vertex vertex : vertexs) {
            System.out.println(vertex.value);
        }
    }

    public void printGraph() {

        for (int[] agdge : edges) {
            System.out.println(Arrays.toString(agdge));
        }

    }


}
